package cn.itcast.es;

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;

import java.io.IOException;

public class ES_Doc_Query {
    public static void main(String[] args) throws IOException {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("192.168.20.129", 9200, "http"))
        );

        // 全量查询
        SearchRequest searchRequest = new SearchRequest("user");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

//        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
//        searchRequest.source(searchSourceBuilder);
//
//        SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
//        System.out.println(search.getTook());
//        search.getHits().forEach(hit -> {
//            System.out.println("查询结果:"+hit.getSourceAsString());
//        });


        // -----------------------------------分割线-------------------------------------

        // 条件查询
//        searchSourceBuilder.query(QueryBuilders.termQuery("age", 20));
//        searchRequest.source(searchSourceBuilder);
//        SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
//        SearchHits hits = response.getHits();
//        hits.forEach(hit -> {
//            System.out.println(hit.getSourceAsString());
//        });

        // -----------------------------------分割线-------------------------------------

//        // 多条件查询
//        SearchSourceBuilder query = searchSourceBuilder.query(QueryBuilders.matchAllQuery());
//        // 当前页码
//        query.from(0);
//        // 每页大小
//        query.size(3);
//        // 根据年龄排序
//        query.sort("age", SortOrder.DESC);
//        // 过滤字段
//        String[] excludes= {};
//        // 包含字段
//        String[] includes = {"name"};
//        query.fetchSource(includes, excludes);
//        searchRequest.source(query);
//
//        SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
//        SearchHits hits = response.getHits();
//        hits.forEach(hit -> {
//            System.out.println(hit.getSourceAsString());
//        });

        // -----------------------------------分割线-------------------------------------

//        // 组合查询   must:and  should:or
//        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
////        boolQueryBuilder.must(QueryBuilders.matchQuery("name", "张三"));
//        boolQueryBuilder.should(QueryBuilders.matchQuery("name", "李四"));
//        boolQueryBuilder.should(QueryBuilders.matchQuery("name", "张三"));
//        SearchSourceBuilder query = searchSourceBuilder.query(boolQueryBuilder);
//
//        searchRequest.source(query);
//
//        SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
//        SearchHits hits = response.getHits();
//        hits.forEach(hit -> {
//            System.out.println(hit.getSourceAsString());
//        });

        // -----------------------------------分割线-------------------------------------

        // 范围查询   must:and  should:or
        RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("age");
        rangeQuery.lte(20);
        SearchSourceBuilder query = searchSourceBuilder.query(rangeQuery);

        searchRequest.source(query);

        SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
        SearchHits hits = response.getHits();
        hits.forEach(hit -> {
            System.out.println(hit.getSourceAsString());
        });



        // 关闭客户端
        client.close();
    }
}
